package com.dkm.biz.gl.domain;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.dkm.base.core.helper.SpringHelper;
import com.dkm.base.core.util.JsonUtil;
import com.dkm.biz.domain.AbstractDomain;
import com.dkm.biz.gl.persistence.dao.EavSetAttrDao;
import com.dkm.biz.gl.persistence.dao.EavSetDao;
import com.dkm.biz.gl.persistence.dao.EavSetGroupDao;
import com.dkm.biz.gl.persistence.entity.EavSetAttrPo;
import com.dkm.biz.gl.persistence.entity.EavSetGroupPo;
import com.dkm.biz.gl.persistence.entity.EavSetPo;

/**
 * 对象功能:属性集 领域对象实体
 * 开发公司:SKG.COM
 * 开发人员:Yuki
 * 创建时间:2014-12-13 16:44:11
 */
@Service
@Scope("prototype")
public class EavSet extends AbstractDomain<String, EavSetPo>{
	 
	 private EavSetDao eavSetDao = null;
	 
	 private EavSetGroupDao eavSetGroupDao = null;
	 
	 private EavSetAttrDao eavSetAttrDao = null;	 
	 
	protected void init(){
		eavSetDao = SpringHelper.getBean(EavSetDao.class);
		eavSetGroupDao = SpringHelper.getBean(EavSetGroupDao.class);
		eavSetAttrDao = SpringHelper.getBean(EavSetAttrDao.class);
		setDao(eavSetDao);
	}
	
	public void saveWithGroupAttr(String userId){
		EavSetPo eavSetPo = getData();
		boolean isNew = StringUtils.isEmpty(eavSetPo.getId())?true:false;
		if(isNew){
			eavSetPo.setCreateBy(userId);
		}else{
			eavSetPo.setUpdateBy(userId);
		}
		//保存主表
		//eavSetPo.setCreateBy("yuki");//TODO tobe deleted
		super.save();
		//解析界面的json，分别保存到属性分组和属性集属性表中
		String eavGroupAttrJson = eavSetPo.getGroupAttrJson();
		
		Map<String,Object> map=new HashMap<String,Object>();
		map.put("children", EavGroupAttr.class);
		List<EavGroupAttr> EavGroupAttrlist =JsonUtil.getDTOList(eavGroupAttrJson, EavGroupAttr.class,map);
		List<EavGroupAttr> processedList = new ArrayList<EavGroupAttr>();
		//delete old data first by set id
		Map<String,Object> params=new HashMap<String,Object>();
		params.put("setId",eavSetPo.getId());
	
		//delete old data
		eavSetAttrDao.deleteByKey("deleteBySetId", params);
		eavSetGroupDao.deleteByKey("deleteBySetId", params);
		
		int eavGroupCount =0;
		int attrCount = 0;
		for(EavGroupAttr eavGroupAttr:EavGroupAttrlist){
			if(attrCount==0){
				if(!StringUtils.isEmpty(eavGroupAttr.get__status()))
				{
					if("delete".equals(eavGroupAttr.get__status())) continue;
				}
				//System.out.println(e.getId());
				EavSetGroupPo eavSetGroupPo = new EavSetGroupPo();
				eavSetGroupPo.setId(getIdGenerator().genSid());
				eavSetGroupPo.setSetId(eavSetPo.getId());
				eavSetGroupPo.setName(eavGroupAttr.getName());
				eavSetGroupPo.setSort(eavGroupCount);//TODO 
				eavGroupCount++;
				eavSetGroupDao.create(eavSetGroupPo);
				List<EavGroupAttr> eavAttrList=eavGroupAttr.getChildren();
				if(eavAttrList!=null){
					for(EavGroupAttr e1:eavAttrList)
					{
						//
						if(!StringUtils.isEmpty(e1.get__status()))
						{
							if("delete".equals(e1.get__status())) continue;
						}
						attrCount++;
						EavSetAttrPo eavSetAttrPo = new EavSetAttrPo();
						eavSetAttrPo.setId(getIdGenerator().genSid());
						eavSetAttrPo.setAttrId(e1.getId());
						eavSetAttrPo.setGroupId(eavSetGroupPo.getId());
						eavSetAttrPo.setSetId(eavSetPo.getId());
						eavSetAttrPo.setSort(attrCount);
						if(StringUtils.isEmpty(e1.getIsMainSku()))e1.setIsMainSku("N");
						else if(e1.getIsMainSku()=="true")e1.setIsMainSku("Y");
						else e1.setIsMainSku("N");
						
						if(e1.isMainSkuFlag()==false)e1.setIsMainSku("N");
						else if(e1.isMainSkuFlag()==true)e1.setIsMainSku("Y");
					
						
						eavSetAttrPo.setIsMainSku(e1.getIsMainSku());
						eavSetAttrDao.create(eavSetAttrPo);	
					}
				}
			}else{
				attrCount--;
			}
			
		}
	}
	public void deleteWithGroupAttr(String id){
		Map<String,Object> params=new HashMap<String,Object>();
		params.put("setId",id);
	
		eavSetAttrDao.deleteByKey("deleteBySetId", params);
		eavSetGroupDao.deleteByKey("deleteBySetId", params);
		super.destroy(id);
	}
	 
}
